package com.zh.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.zh.common.BaseResponse;
import com.zh.common.ErrorCode;
import com.zh.common.ResultUtils;
import com.zh.domain.dto.iteration.AddIterationRequest;
import com.zh.domain.dto.iteration.UpdateIterationRequest;
import com.zh.domain.vo.iteration.IterationVo;
import com.zh.domain.vo.task.TaskCountVo;
import com.zh.exception.ThrowUtils;
import com.zh.service.IterationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "IterationController", value = "迭代控制器")
@RestController
@RequestMapping("/iteration")
@Slf4j
@SaCheckLogin
public class IterationController {
    @Resource
    private IterationService iterationService;

    @PostMapping("/add")
    @ApiOperation(value = "addIteration", notes = "新建迭代")
    public BaseResponse<Boolean> addIteration(@RequestBody AddIterationRequest addIterationRequest) {
        ThrowUtils.throwIf(null == addIterationRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = iterationService.addIteration(addIterationRequest);
        return ResultUtils.success(result);
    }

    @PutMapping("/update")
    @ApiOperation(value = "updateIteration", notes = "更新迭代")
    public BaseResponse<Boolean> updateIteration(@RequestBody UpdateIterationRequest updateIterationRequest) {
        ThrowUtils.throwIf(null == updateIterationRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = iterationService.updateIteration(updateIterationRequest);
        return ResultUtils.success(result);
    }
    @GetMapping
    @ApiOperation(value = "getIteration", notes = "id获取迭代详情")
    public BaseResponse<IterationVo> getIteration(Integer id){
        ThrowUtils.throwIf(null == id, ErrorCode.PARAMS_ERROR);
        IterationVo result = iterationService.getDetail(id);
        return ResultUtils.success(result);
    }

    @GetMapping("/taskCount")
    @ApiOperation(value = "getTaskCount", notes = "获取迭代任务统计信息")
    public BaseResponse<TaskCountVo> getTaskCountByIterId(Integer iterId) {
        TaskCountVo taskCountVo = iterationService.getTaskCount(iterId);
        return ResultUtils.success(taskCountVo);
    }
}
